package com.hw.core.system.controller;

import com.hw.core.system.entity.Course;
import com.hw.core.system.entity.CourseType;
import com.hw.core.system.service.CourseService;
import com.hw.core.system.service.CourseTypeService;
import com.hw.core.util.UuidUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("courseType")
public class CourseTypeController {

    @Autowired
    private CourseTypeService courseTypeService;

    @RequiresPermissions(value = "课程类型查询")
    @GetMapping("list")
    public String list(Model model) {
        List<CourseType> courseTypes = courseTypeService.findAllCourseType();
        model.addAttribute( "courseTypes", courseTypes );
        return "courseType/listCourseType";

    }

    @RequiresPermissions(value = "课程类型新增")
    @GetMapping("add")
    public String add() {
        return "courseType/addCourseType";
    }

    @RequiresPermissions(value = "课程类型新增")
    @PostMapping("add")
    @ResponseBody
    public String add(CourseType courseType) {
        CourseType add = courseTypeService.add( courseType );
        if (add != null) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    @RequiresPermissions(value = "课程类型修改")
    @GetMapping("edit")
    public String edit(Model model, Integer id) throws Exception {
        if (id == null)
            throw new Exception( "参数错误" );
        model.addAttribute( "courseType", courseTypeService.findCourseTypeById( id ) );
        return "courseType/editCourseType";
    }

    @RequiresPermissions(value = "课程类型修改")
    @PostMapping("edit")
    @ResponseBody
    public String edit(CourseType courseType) {
        courseTypeService.update( courseType );
        return "修改成功";

    }

    @RequiresPermissions(value = "课程类型删除")
    @PostMapping("delete")
    @ResponseBody
    public String delete(Integer id) {

        courseTypeService.delete( id );
        return "修改成功";

    }

}
